<?php
class account extends adminController {
    public $_index = 'admin/account/index';
    public $_listView = 'admin/account/list';
    public $_addView = 'admin/account/add';
    public $_editView = 'admin/account/edit';
    
	public function __construct() {
		parent::__construct ();
        $this->session->set_userdata('selectedMenuAdmin', 'account');
	}
	
    public function loadView($temp) {
        if (!isset($temp['data']))
            $temp['data'] = '';
        $this->load->view('admin/template', $temp);
    }
    
    public function index() {
        $this->_data['template'] = $this->_listView;
        $this->_data['data'] = $this->accountModel->get();
        $this->loadView($this->_data);
    }
    
    public function add() {
        $this->_data['template'] = $this->_addView;
        if(!$this->isPost()) {
            $this->loadView($this->_data);
            return;
        }
        $data = $this->input->post();
        $data['passwordd'] = md5($data['passwordd']);
        $this->accountModel->insert($data);
        
        redirect(base_url().$this->_index);
    }
    
    public function edit($id) {
        $this->_data['template'] = $this->_editView;
        if(!$this->isPost()) {
            $this->_data['data'] = $this->accountModel->read($id);
            $this->loadView($this->_data);
            return;   
        }
        $data = $this->input->post();
        $data['passwordd'] = md5($data['passwordd']);
        $this->accountModel->update($data, array('id' => $id));
        redirect(base_url().$this->_index);
    }
    
    public function delete($id) {
        $acc = $this->accountModel->read($id);
        if($acc['username'] != 'admin') $this->accountModel->delete($id);
        redirect(base_url().$this->_index);
    }
}
?>